<?php
class ProductController extends BaseController {
    protected $layout = 'layouts.admin';
	public function index()
	{
		$products  = App::make('Product')->paginate(Config::get('app.page_row'));
		$this->layout->content = View::make('admin.product.index')->with(compact('products'));
	}
	
	public function add()
	{
		if ( Input::server('REQUEST_METHOD') == 'POST'){
			$data = Input::get('data');
			if (  Input::hasFile('upload_pic') ) {
				$upload_result  = UploadHelper::uploadImage('upload_pic', 'uploads',false);
				if ($upload_result['error'] == 0){					
					$data['picture'] = $upload_result['files'][0]['path'];
					$data['thumb'] = $upload_result['files'][0]['path'];
				}
				
			}else{
				$data['picture'] = $data['thumb'] = '';
			}
						
			$Pro = new Product();
			$data['market_price'] = 0;
			$data['sort'] = 0;
			$data['status'] = 0;
			foreach ($data as $k=>$v ){
				$Pro->$k = $v;
			}
			$Pro->save();
			if ($Pro->id) {
				$PA = new ProductAttribute();
				$PA->product_id = $Pro->id;
				$attr = Input::get('attribute');
				$PA->introduce = $attr['introduce'];
				$PA->save();
				
				$specs = Input::get('specs');
				if ( count($specs)){
					foreach ($specs as $spid=>$spval ) {
						$ps = new ProductSpec();
						$ps->product_id = $Pro->id;
						$ps->specificant_id = $spid;
						if(is_array($spval)){
							$ps->value  = join(',', $spval);
						}else{
							$ps->value  = $spval;
						}						
						$ps->save();
					}					
				}
			}
			return Redirect::to( route('admin.product.index') );
		}else {			
			$categories = ProductCategory::all();
			$this->layout->content = View::make('admin.product.add')->with(compact('categories'));
		}
		
	}
	
	public function edit($id)
	{
		$pro = Product::find($id);
		if ( Input::server('REQUEST_METHOD') == 'POST'){
			$data = Input::get('data');
			if ( Input::hasFile('upload_pic')) {
				$upload_result  = UploadHelper::uploadImage('upload_pic', 'uploads',TRUE, ImageHelper::SIZE_BIG);
				if ($upload_result['error'] == 0){					
					$data['picture'] = $upload_result['files'][0]['path'];
					$data['thumb'] = $upload_result['files'][0]['thumb'];
				}
		
			}
		
			foreach ($data as $k=>$v ){
				$pro->$k = $v;
			}
			$pro->save();
			$attr = Input::get('attribute');
			if( $pro->attribute ){
				$pro->attribute->introduce = $attr['introduce'];
				$pro->attribute->save();
			}
			elseif($pro->id) {
				$PA = new ProductAttribute();
				$PA->product_id = $pro->id;				
				$PA->introduce = $attr['introduce'];
				$PA->save();
			}
			ProductSpec::where('product_id', '=', $pro->id)->delete();
				$specs = Input::get('specs');
				if ( count($specs)){
					foreach ($specs as $spid=>$spval ) {
						$ps = new ProductSpec();
						$ps->product_id = $pro->id;
						$ps->specificant_id = $spid;
						if(is_array($spval)){
							$ps->value  = join(',', $spval);
						}else{
							$ps->value  = $spval;
						}
						$ps->save();
					}
				}
			return Redirect::to( route('admin.product.index') );
		}else {
			$cur_cid  = $pro->category_id;
			$pro_specs  = [];
			foreach( $pro->specs as $pro_spec) {
				$pro_specs[$pro_spec->specificant_id] = $pro_spec;
			}
			$specs = [];
			if ( $cur_cid ){
				$cat = ProductCategory::find($cur_cid);
				$spec_ids = explode(',',$cat->specs);
				if( $cat->specs ){
					$specs = Specificant::whereIn('id',$spec_ids)->get();
				}
			}
			$categories = ProductCategory::all();
			$this->layout->content = View::make('admin.product.edit')->with(compact('categories','pro','specs', 'pro_specs'));
		}
	}
	
	public function drop($id)
	{
		$product = Product::find($id);
		if($product){
			ProductSpec::where('product_id', $id)->delete();
			ProductAttribute::where('product_id', $id)->delete();
			$product->delete();
			return Redirect::back()->with('success', '删除成功。');
		}else{
			return Redirect::back()->with('error', '没有找到。');
		}
	}
	
	public function cat_spec($id)
	{
		$specs = [];
		if ( $id ){
			$cat = ProductCategory::find($id);
			$spec_ids = explode(',',$cat->specs);
			if( $spec_ids ){
				$specs = Specificant::whereIn('id',$spec_ids)->get();
			}
		}
				
		return View::make('admin.product.cat_spec')->with(compact('specs'));
	}

}